<template>
  <section>
    <a-spin tip="加载中..." class="position" v-if="loading"> </a-spin>
    <Button class="search_btns" type="search" label="查询" @click="getReportCabinetStatusInterval"></Button>
    <div class="box">
      <a-form-model
        ref="stateForm"
        :model="stateForm"
        :rules="stateRules"
        :label-col="{ span: 10 }"
        :wrapper-col="{ span: 14 }"
      >
        <a-card title="属性上报时间间隔">
          <template #extra>
            <Button
              type="search"
              label="设置"
              style="margin-left: 8px"
              @click="setCabinetReportStatusInterval"
            ></Button>
          </template>
          <a-row type="flex" justify="space-between">
            <a-col :span="12">
              <InputNumber
                :form="stateForm"
                label="属性增量上报间隔"
                prop="06041801"
                placeholder="请输入"
                suffix="秒"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="stateForm"
                label="属性全量上报间隔"
                prop="06041901"
                placeholder="请输入"
                suffix="秒"
              ></InputNumber>
            </a-col>
          </a-row>
        </a-card>
        <br />
      </a-form-model>
      <a-form-model ref="versionForm" :model="versionForm" :label-col="{ span: 10 }" :wrapper-col="{ span: 14 }">
        <a-card title="版本">
          <a-col :span="24">
            <a-col :span="12">
              <InputNumber :form="versionForm" label="主控版本号" prop="06040501" readonly :precision="1"></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber :form="versionForm" label="仓控版本号" prop="06040601" readonly :precision="1"></InputNumber>
            </a-col>
          </a-col>

          <a-col :span="12">
            <InputNumber :form="versionForm" label="柜控版本号" prop="06040701" readonly :precision="1"></InputNumber>
          </a-col>
        </a-card>
      </a-form-model>
      <a-form-model
        ref="configureForm"
        :model="configureForm"
        :rules="configureRules"
        :label-col="{ span: 10 }"
        :wrapper-col="{ span: 14 }"
      >
        <a-card title="充电柜参数配置">
          <template #extra>
            <!-- <Button type="search" @click="getCabinetParams"></Button> -->
            <Button type="search" label="设置" style="margin-left: 8px" @click="setCabinetParams"></Button>
          </template>

          <a-row type="flex" justify="space-between">
            <a-col :span="12">
              <InputNumber
                :form="configureForm"
                label="总功率限制值"
                prop="06040101"
                placeholder="请输入"
                suffix="W"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="configureForm"
                label="系统散热部件启动阈值"
                prop="06040201"
                placeholder="请输入"
                suffix="℃"
              ></InputNumber>
            </a-col>
            <!-- <a-col :span="12">
              <InputNumber
                :form="configureForm"
                label="时间流逝基准电流"
                prop="timeLapseReferenceCurrent"
                placeholder="请输入"
                suffix="A"
                :precision="1"
              ></InputNumber>
            </a-col> -->

            <!-- <a-col :span="12">
              <InputNumber
                :form="configureForm"
                label="涓流状态判断"
                prop="trickleStatePercent"
                placeholder="请输入"
                suffix="%"
              ></InputNumber>
            </a-col> -->

            <a-col :span="12">
              <InputNumber
                :form="configureForm"
                label="输出过流阈值"
                prop="06043101"
                placeholder="请输入"
                :precision="1"
                suffix="A"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber :form="configureForm" label="发起充电超时时长" prop="06042001" suffix="秒"></InputNumber>
            </a-col>
            <a-col :span="12">
              <Select
                :form="configureForm"
                label="语音播报使能"
                prop="06040301"
                :optList="[
                  { name: '开启', code: '1' },
                  { name: '关闭', code: '2' },
                ]"
                placeholder="请选择"
                @change="voiceChange"
              ></Select>
            </a-col>
            <!-- <a-col :span="12">
              <Select
                :form="configureForm"
                label="时间流逝功能使能"
                prop="timeLapse"
                :optList="[
                  { name: '开启', code: 1 },
                  { name: '关闭', code: 2 },
                ]"
                placeholder="请选择"
              ></Select>
            </a-col> -->
            <a-col :span="24" v-if="voiceShow">
              <a-col :span="24">
                <a-col :span="12">
                  <InputNumber
                    :form="configureForm"
                    label="语音播报时间段个数"
                    prop="06040401"
                    placeholder="请输入"
                    @change="setTimeNumber"
                  ></InputNumber>
                </a-col>
              </a-col>
              <a-col :span="12" v-for="(domain, index) in configureForm.timeSlices" :key="index">
                <rangeInputNumber
                  :form="domain"
                  :label="'时间段' + (index + 1)"
                  :name="'timeSlices.' + index + '.start&stop'"
                  prop="start&stop"
                ></rangeInputNumber>
              </a-col>
            </a-col>

            <!-- <a-col :span="12">
              <Input :form="configureForm" label="校时" prop="06041701"></Input>
            </a-col> -->

            <!-- <a-col :span="12">
              <Select
                :form="configureForm"
                label="柜订单状态"
                prop="06042101"
                :optList="[
                  { name: '续充', code: 0 },
                  { name: '充电完成', code: 1 },
                ]"
                placeholder="请选择"
              ></Select>
            </a-col> -->
            <a-col :span="12">
              <InputNumber
                :form="configureForm"
                label="最大充电电量"
                prop="06043601"
                placeholder="请输入"
                :step="0.1" :precision="1"
                :min="0"
                suffix="KW·h"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="configureForm"
                label="停充电流百分比"
                prop="06043701"
                placeholder="请输入"
                :min="0" :max="100"
                suffix="%"
              ></InputNumber>
            </a-col>
          </a-row>
        </a-card>
      </a-form-model>
      <br />

      <a-form-model
        ref="cabinForm"
        :model="cabinForm"
        :rules="cabinRules"
        :label-col="{ span: 10 }"
        :wrapper-col="{ span: 14 }"
      >
        <a-card title="充电仓常用参数配置">
          <template #extra>
            <Button type="search" label="设置" @click="getElectricityMeter"></Button>
          </template>
          <a-row type="flex" justify="space-between">
            <a-col :span="12">
              <InputNumber
                :form="cabinForm"
                label="插座断开时间"
                prop="06042201"
                placeholder="请输入"
                suffix="秒"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="cabinForm"
                label="过功率阈值"
                prop="06042301"
                placeholder="请输入"
                suffix="W"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="cabinForm"
                label="涓流状态判断"
                prop="06042401"
                placeholder="请输入"
                suffix="W"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="cabinForm"
                label="最长输出时间"
                prop="06042501"
                placeholder="请输入"
                suffix="秒"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="cabinForm"
                label="涓流充电最长时间"
                prop="06042601"
                placeholder="请输入"
                suffix="秒"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="cabinForm"
                label="输出欠压阈值"
                prop="06042701"
                placeholder="请输入"
                suffix="V"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="cabinForm"
                label="输出过压阈值"
                prop="06042801"
                placeholder="请输入"
                suffix="V"
              ></InputNumber>
            </a-col>

            <a-col :span="12">
              <InputNumber
                :form="cabinForm"
                label="高温阈值"
                prop="06042901"
                placeholder="请输入"
                suffix="℃"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="cabinForm"
                label="低温阈值"
                prop="06043001"
                placeholder="请输入"
                suffix="℃"
              ></InputNumber>
            </a-col>
            <!-- <a-col :span="12">
              <InputNumber :form="cabinForm" label="充电时长" prop="06043201" :precision="1" suffix="秒"></InputNumber>
            </a-col> -->
            <!-- <a-col :span="12">
              <Select
                :form="cabinForm"
                label=" 计费模式"
                prop="06043301"
                :optList="[
                  { name: '按时间', code: 1 },
                  { name: '按电量', code: 2 },
                  { name: '按功率', code: 3 },
                ]"
                placeholder="请选择"
              ></Select>
            </a-col> -->

            <!-- <a-col :span="12">
              <InputNumber
                :form="systemMeterForm"
                label="有功电量"
                prop="activePower"
                placeholder=" "
                readonly
                suffix="kW·h"
                :precision="2"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="systemMeterForm"
                label="电压"
                prop="voltage"
                placeholder=" "
                readonly
                suffix="V"
                :precision="1"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="systemMeterForm"
                label="电流"
                prop="current"
                placeholder=" "
                readonly
                suffix="A"
                :precision="2"
              ></InputNumber>
            </a-col>
            <a-col :span="12">
              <InputNumber
                :form="systemMeterForm"
                label="功率"
                prop="power"
                placeholder=" "
                readonly
                suffix="W"
              ></InputNumber>
            </a-col> -->
          </a-row>
        </a-card>
      </a-form-model>
      <a-form-model
        v-if="chargeCabintForm.list.length > 0"
        ref="chargeCabintForm"
        :model="chargeCabintForm"
        :label-col="{ span: 10 }"
        :wrapper-col="{ span: 14 }"
      >
        <a-card title="充电仓参数配置">
          <template #extra>
            <Button type="search" label="设置" @click="isChargeCabint" :loading="cabintBtnLoading"></Button>
          </template>

          <a-row type="flex" justify="space-between" v-if="cabintMsg">
            <p style="color:red;">* 请选择需要设置的锁常开常闭状态！</p>
          </a-row>
          <a-row type="flex" justify="space-between">
            <a-col :span="12" v-for="(item, index) in chargeCabintForm.list" :key="index">
              <Select
                :form="item"
                :label="`仓${item.chargingCabinetBinIndex}：锁常开常闭`"
                prop="value"
                :optList="[
                  { name: '请选择', code: 'null' },
                  { name: '高电平', code: '1' },
                  { name: '低电平', code: '0' },
                ]"
                placeholder="请选择"
              ></Select>
            </a-col>
          </a-row>
        </a-card>
      </a-form-model>
      <!-- 20220111z 注释原因没有接口 -->
      <!-- <br />
      <a-card title="单卡查询">
        <template #extra>
          <Button type="search"></Button>
        </template>
        <a-form-model
          ref="readOnlyForm"
          :model="readOnlyForm"
          :label-col="{ span: 10 }"
          :wrapper-col="{ span: 14 }"
        >
          <a-row type="flex" justify="space-between">
            <a-col :span="24">
              <a-col :span="12">
                <Input
                  :form="readOnlyForm"
                  label="查询iccid号"
                  prop="searchIccid"
                ></Input>
              </a-col>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="iccid"
                prop="a"
                placeholder=" "
                readonly
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="激活时间"
                prop="a"
                placeholder=" "
                readonly
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="失效时间"
                prop="a"
                placeholder=" "
                readonly
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="流量总量"
                prop="a"
                placeholder=" "
                readonly
                suffix="KB"
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="流量使用量"
                prop="a"
                placeholder=" "
                readonly
                suffix="KB"
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="流量剩余量"
                prop="a"
                placeholder=" "
                readonly
                suffix="KB"
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="本月流量使用量"
                prop="a"
                placeholder=" "
                readonly
                suffix="KB"
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="卡状态"
                prop="a"
                placeholder=" "
                readonly
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="卡在线状态"
                prop="a"
                placeholder=" "
                readonly
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="设备开关机状态"
                prop="a"
                placeholder=" "
                readonly
              ></Input>
            </a-col>
            <a-col :span="12">
              <Input
                :form="readOnlyForm"
                label="主套餐名称"
                prop="a"
                placeholder=" "
                readonly
              ></Input>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card> -->
    </div>
  </section>
</template>
<script>
import {
  querySoftwareDownResult,
  sendCabinetLoginInfo,
  querySoftwareVersion,
  queryICCID,
  queryCabinetSecretKey,
  queryCabinetLog,
  queryCabinetParams,
} from '@/api/api';
import moment from 'moment';
import { frontToAfterTransformation } from '@/utils/tool';
import typicalSearch from '@/components/typicalSearch';
import typicalTable from '@/components/typicalTable';
import Input from '@/components/typicalControl/Input';
import InputNumber from '@/components/typicalControl/InputNumber';
import treeSelect from '@/components/typicalControl/treeSelect';
import Select from '@/components/typicalControl/select';
import Button from '@/components/typicalControl/button';
import datePicker from '@/components/typicalControl/datePicker';
import rangeInputNumber from '@/components/typicalControl/rangeInputNumber';

import hourTimePicker from '@/components/typicalControl/hourTimePicker';
import { forEach } from 'store/storages/all';
import { listParameterSending } from '@/api/cabin';

export default {
  name: 'ParameterOperation',
  components: {
    typicalSearch: typicalSearch,
    typicalTable: typicalTable,
    Input: Input,
    InputNumber: InputNumber,
    treeSelect: treeSelect,
    Select: Select,
    Button: Button,
    datePicker: datePicker,
    rangeInputNumber: rangeInputNumber,
    hourTimePicker: hourTimePicker,
  },
  props: {
    id: {
      type: String,
      default: '',
    },
    details: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      cabintMsg: false,
      cabintBtnLoading:false,
      voiceShow: false,
      // 获取日志
      logForm: {
        ftpAddress: '124.207.112.70',
        ftpUsername: 'dpcpower',
        ftpPassword: 'dpcpower',
        ftpLogPath: undefined,
        text: undefined,
      },
      logRules: {
        ftpAddress: [
          { required: true, message: '请输入FTP地址', trigger: 'blur' },
          { max: 45, message: '最大长度不能超过45个字', trigger: 'blur' },
        ],
        ftpUsername: [
          { required: true, message: '请输入FTP用户名', trigger: 'blur' },
          { max: 45, message: '最大长度不能超过45个字', trigger: 'blur' },
        ],
        ftpPassword: [
          { required: true, message: '请输入FTP密码', trigger: 'blur' },
          { max: 45, message: '最大长度不能超过45个字', trigger: 'blur' },
        ],
        ftpLogPath: [
          { required: true, message: '请输入FTP存放日志地址', trigger: 'blur' },
          { max: 45, message: '最大长度不能超过45个字', trigger: 'blur' },
        ],
      },
      // 状态
      stateForm: {
        '06041801': undefined,
        '06041901': undefined,
      },
      stateRules: {
        '06041801': [{ required: true, message: '请输入属性增量上报间隔', trigger: 'blur' }],
        '06041901': [{ required: true, message: '请输入属性全量上报间隔', trigger: 'blur' }],
      },
      versionForm: {
        '06040501': '',
        '06040601': '',
        '06040701': '',
      },
      cabinForm: {
        '06042201': undefined,
        '06042301': undefined,
        '06042401': undefined,
        '06042501': undefined,
        '06042601': undefined,
        '06042701': undefined,
        '06042801': undefined,
        // '06043201': undefined,
        // '06043301': '',
        '06043401': '',
        '06042901': '',
        '06043001': '',
      },
      cabinRules: {
        '06042201': [{ required: true, message: '请输入插座断开时间', trigger: 'blur' }],
        '06042301': [{ required: true, message: '请输入过功率阈值', trigger: 'blur' }],
        '06042401': [{ required: true, message: '请输入涓流状态判断(按功率)', trigger: 'blur' }],
        '06042501': [{ required: true, message: '请输入最长输出时间', trigger: 'blur' }],
        '06042601': [{ required: true, message: '请输入涓流充电最长时间', trigger: 'blur' }],
        '06042701': [{ required: true, message: '请输入输出欠压阈值', trigger: 'blur' }],
        '06042801': [{ required: true, message: '请输入输出过压阈值', trigger: 'blur' }],
        // '06043201': [{ required: true, message: '请输入充电时长', trigger: 'blur' }],
        // '06043301': [{ required: true, message: '请选择计费模式', trigger: 'change' }],
        // '06043401': [{ required: true, message: '请选择锁状态', trigger: 'blur' }],
        '06042901': [{ required: true, message: '请输入高温阈值', trigger: 'blur' }],
        '06043001': [{ required: true, message: '请输入低温阈值', trigger: 'blur' }],
      },
      // 配置
      configureForm: {
        '06040101': undefined,
        '06040201': undefined,
        // timeLapseReferenceCurrent: undefined,
        // trickleStatePercent: undefined,

        '06043101': undefined,
        '06040301': undefined,
        // timeLapse: undefined,
        '06040401': undefined,
        timeSlices: [],
        // '06041701': '',
        '06042001': '',
        // '06042101': '',
        '06043601': undefined,
        '06043701': undefined,
      },
      chargeCabintForm: {
        list: [],
      }, //仓list
      configureRules: {
        '06040101': [{ required: true, message: '请输入总功率限制值', trigger: 'blur' }],
        '06040201': [{ required: true, message: '请输入系统散热部件启动阈值', trigger: 'blur' }],
        // timeLapseReferenceCurrent: [{ required: true, message: '请输入时间流逝基准电流', trigger: 'blur' }],
        // trickleStatePercent: [{ required: true, message: '请输入涓流状态判断(按百分比) ', trigger: 'blur' }],

        '06043101': [{ required: true, message: '请输入输出过流阈值', trigger: 'blur' }],
        '06040301': [{ required: true, message: '请选择语音播报使能', trigger: 'change' }],
        // timeLapse: [{ required: true, message: '请选择时间流逝功能使能', trigger: 'change' }],
        '06040401': [{ required: true, message: '请输入语音播报时间段个数', trigger: 'blur' }],
        // '06042101': [{ required: true, message: '请选择', trigger: 'change' }],
        '06042001': [{ required: true, message: '请输入发起充电超时时长', trigger: 'blur' }],
        '06043601': [{ required: true, message: '请输入最大充电电量', trigger: 'blur' }],
        '06043701': [{ required: true, message: '请输入停充电流百分比', trigger: 'blur' }],
        // '06041701': [{ required: true, message: '请输入校时', trigger: 'blur' }],
      },
      // 密钥
      secretKeyForm: {
        secretKey: undefined,
        lastModifyTime: undefined,
      },
      secretKeyRules: {
        // secretKey: [{ required: true, message: '请输入密钥', trigger: 'blur' }]
      },
      // 系统电表
      systemMeterForm: {
        activePower: undefined,
        voltage: undefined,
        current: undefined,
        power: undefined,
      },
      // 仅查询使用字段
      readOnlyForm: {
        version: undefined,
        iccid: undefined,
        searchIccid: undefined,
        text: undefined,
      },
      // 用户密码
      loginForm: {
        password: undefined,
        username: undefined,
      },
      loginRules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { max: 45, message: '最大长度不能超过45个字', trigger: 'blur' },
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { max: 45, message: '最大长度不能超过45个字', trigger: 'blur' },
        ],
      },
      queryForm: {
        packageName: undefined,
      },
      queryRules: {
        packageName: [
          { required: true, message: '请输入软件版本号', trigger: 'blur' },
          { max: 45, message: '最大长度不能超过45个字', trigger: 'blur' },
        ],
      },
      loading: false,
    };
  },
  created() {
    this.getReportCabinetStatusInterval();
  },
  mounted() {},
  methods: {
    moment,
    voiceChange(e) {
      this.voiceShow = e == 1 ? true : false;
    },
    detealParam() {
      this.details.cabinetParams.forEach((item) => {
        for (const key in this.configureForm) {
          if (key == item.protocolSemaphoreCode) {
            this.configureForm[key] = item.value;
          }
        }
        for (const key in this.stateForm) {
          if (key == item.protocolSemaphoreCode) {
            this.stateForm[key] = item.value;
          }
        }
        for (const key in this.cabinForm) {
          if (key == item.protocolSemaphoreCode) {
            this.cabinForm[key] = item.value;
          }
        }
        for (const key in this.versionForm) {
          if (key == item.protocolSemaphoreCode) {
            this.versionForm[key] = item.value;
          }
        }
      });
      this.chargeCabintForm.list = this.details.cabinetParams.filter(
        (item) => item.protocolSemaphoreCode == '06043401'
      );
    },
    addParamConfirm(e) {
      let params = [];
      for (const key in e) {
        if (key == 'timeSlices') {
          let timesArr = '';
          e.timeSlices.map((pp, inx) => {
            timesArr += inx == e.timeSlices.length - 1 ? pp['start&stop'].join(',') : pp['start&stop'].join(',') + '|';
          });
          params.push({
            protocolSemaphoreCode: '06040401',
            value: timesArr,
          });
        } else if (key != '06040401') {
          params.push({
            protocolSemaphoreCode: key,
            value: e[key],
          });
        }
      }

      let datas = {
        chargingCabinetId: this.id,
        params: params,
      };
      listParameterSending('/write', 'post', datas)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success('参数设置成功');
            this.parameterVisible = false;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    isChargeCabint(){
      this.$confirm({
        title: '提示',
        content: (h) => '确定要设置已选充电仓参数吗？',
        onOk: () => this.doChargeCabint(),
        centered: true,
        maskClosable: true,
      });
    },
    // 充电仓参数设置
    doChargeCabint() {
      this.cabintBtnLoading = true;
      let param = this.chargeCabintForm.list.filter((item) => item.value != 'null');
      this.cabintMsg = param.length == 0 ? true : false;
      if (param.length == 0) return;

      let cabints = param.map((item) => {
        return {
          protocolSemaphoreCode: item.protocolSemaphoreCode,
          value: item.value,
          chargingCabinetBinIndex: item.chargingCabinetBinIndex,
        };
      });
      let datas = {
        chargingCabinetId: this.id,
        params: cabints,
      };
      listParameterSending('/write', 'post', datas)
        .then((res) => {
          if (res.code == 200) {
            this.$message.success('参数设置成功');
            this.parameterVisible = false;
          }
        })
        .finally(() => {
          this.cabintBtnLoading = false;
        });
    },
    /**
     * [getCabinetLog 获取充电柜日志]
     * @return {[type]} [description]
     */
    getCabinetLog() {
      this.$refs.logForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          queryCabinetLog({ id: this.id, body: this.logForm })
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.message);
                var text = '';
                for (var key in res.data) {
                  text = text + key + ':' + res.data[key] + ' ';
                }
                this.logForm.text = text;
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },

    /**
     * [getCabinetParams 查询充电柜参数]
     * @return {[type]} [description]
     */
    getCabinetParams() {
      this.loading = true;
      queryCabinetParams({ id: this.id })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            Object.assign(this.configureForm, res.data);
            // this.multipleTransformation(this.configureForm, 'timeLapseReferenceCurrent', 0.1);
            this.multipleTransformation(this.configureForm, '06043101', 0.1);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * [setCabinetParams 设置充电柜参数]
     */
    setCabinetParams() {
      this.$refs.configureForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.addParamConfirm(this.configureForm);
        }
      });
    },
    /**
     * [getCabinetSecretKey 获取密钥信息]
     * @return {[type]} [description]
     */
    getCabinetSecretKey() {
      this.loading = true;
      queryCabinetSecretKey({ id: this.id })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            Object.assign(this.secretKeyForm, res.data);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * [getElectricityMeter 查询充电柜电表信息]
     * @return {[type]} [description]
     */
    getElectricityMeter() {
      this.$refs.cabinForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.addParamConfirm(this.cabinForm);
        }
      });
    },
    /**
     * [getQueryICCID 查询iccid]
     * @return {[type]} [description]
     */
    getQueryICCID() {
      this.loading = true;
      queryICCID({ id: this.id })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            Object.assign(this.readOnlyForm, res.data);
            this.readOnlyForm.searchIccid = this.readOnlyForm.iccid;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * [getReportCabinetStatusInterval 查询充电柜状态上报时间间隔]
     * @return {[type]} [description]
     */
    getReportCabinetStatusInterval() {
      this.loading = true;
      listParameterSending(`/read?chargingCabinetId=${this.id}`, 'get')
        .then((res) => {
          if (Array.isArray(res)) {
            this.details.cabinetParams = res;
            this.detealParam();
          }
        })
        .finally((com) => {
          this.loading = false;
        });
    },
    /**
     * [setCabinetReportStatusInterval 设置充电柜状态上报时间间隔]
     */
    setCabinetReportStatusInterval() {
      this.$refs.stateForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.addParamConfirm(this.stateForm);
        }
      });
    },
    /**
     * [getSoftwareVersion 查询软件版本号]
     * @return {[type]} [description]
     */
    getSoftwareVersion() {
      this.loading = true;
      querySoftwareVersion({ id: this.id })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            Object.assign(this.readOnlyForm, res.data);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /**
     * [setCabinetLoginInfo 设置用户名与密码]
     */
    setCabinetLoginInfo() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          sendCabinetLoginInfo({ id: this.id, body: this.loginForm })
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.message);
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    /**
     * [getSoftwareDownResult 查询充电柜软件下载是否成功]
     * @return {[type]} [description]
     */
    getSoftwareDownResult() {
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          querySoftwareDownResult({ id: this.id, body: this.queryForm })
            .then((res) => {
              if (res.code == 200) {
                this.$message.success(res.message);
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    /**
     * [setTimeNumber 设置时间段个数(充电柜参数配置)]
     * @param {[type]} e [description]
     */
    setTimeNumber(e) {
      this.$set(this.configureForm, 'timeSlices', []);
      for (var i = 0; i < e; i++) {
        this.configureForm.timeSlices.push({ 'start&stop': [] });
      }
    },
    /**
     * [isTimeIntervalOverlap 是否时间区间重叠（arr格式时间）]
     * @param  {[type]}  obj   [校验对象]
     * @param  {[type]}  aRule [校验规则]
     * @return {Boolean}       [是否]
     */
    isTimeIntervalOverlap(obj, aRule) {
      var aParent, i, j, aFlatten, temporaryObj, k, time1, time2, time3, time4;
      aFlatten = [];
      for (i = 0; i < aRule.length; i++) {
        aParent = obj[aRule[i].parent];
        for (j = 0; j < aParent.length; j++) {
          if (aParent[j][aRule[i].attribute].length < 2) {
            console.log('isTimeIntervalOverlap 时间区间位数不足！');
            // 有必填去校验时间为空
            return false;
          }
          temporaryObj = {};
          temporaryObj.num = j + 1;
          temporaryObj.a = aParent[j][aRule[i].attribute][0];
          temporaryObj.b = aParent[j][aRule[i].attribute][1];
          aFlatten.push(temporaryObj);
        }
        for (j = 0; j < aFlatten.length; j++) {
          for (k = 0; k < aFlatten.length; k++) {
            // 跳过自身比较
            if (k === j) {
              continue;
            }
            // https://ask.csdn.net/questions/1092563
            // 不重叠的规则是：区间A的最大值小于区间B的最小值，或区间A的最小值大于区间B的最大值
            time1 = aFlatten[j].a;
            time2 = aFlatten[j].b;
            time3 = aFlatten[k].a;
            time4 = aFlatten[k].b;
            if (!(time2 <= time3 || time1 >= time4)) {
              const index1 = j + 1;
              const index2 = k + 1;
              this.$message.error('时间段' + index1 + ' 与 时间段' + index2 + ' 重叠');
              return true;
            }
          }
        }
      }
      return false;
    },
    /**
     * [multipleTransformation 转换倍率]
     * @param  {[type]} val          [对象实体]
     * @param  {[type]} attribute    [属性]
     * @param  {[type]} expand       [倍率]
     * @return {[type]}              [description]
     */
    multipleTransformation(val, attribute, expand) {
      if (val[attribute]) {
        val[attribute] = Number(val[attribute]) * expand;
      }
    },
  },
};
</script>

<style scoped>
.position {
  width: 100%;
  height: 100%;
  background: white;
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}
.box {
  position: absolute;
  width: 100%;
  height: 100%;
  /* overflow: auto; */
  z-index: 1;
}
.search_btns {
  display: flex;
  justify-content: flex-end;
  margin: 0 0 10px;
}
</style>
